from flask import Flask
from flask_restful import Api
import requests
from bs4 import BeautifulSoup
import urllib3
import json

app = Flask(__name__)
api = Api(app)


@app.route('/apkcombo/<name>/<package_name>')
def index(name, package_name):
    print("名称：" + name)
    print("包名：" + package_name)
    # 定义目标 URL 和请求头
    url = f"https://apkcombo.com/{name}/{package_name}/01a1200x20240308/dl"
    headers = {
        'Cookie': '__cf_bm=9WmcLoJUK0ViEB8ZoaTWmRgNimpElyTZg_km0zNGTAk-1733994279-1.0.1.1-lxEZxT207ykXGEqeiewOv600QnTBMPjSyh19QVOTru8lYlrbKxbrcNQTFgi7JJtBMfov4xfmWQKsngrqmlziLQ'
    }
    # 定义表单数据
    data = {
        'package_name': package_name,
        'version': ''
    }
    # 禁用代理
    proxies = {
        'http': None,
        'https': None
    }
    # 禁用警告
    urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
    res = {"code": 0, "msg": "请求成功"}
    res_data = []
    try:
        # 发送 POST 请求
        response = requests.post(url, headers=headers, data=data, proxies=proxies, verify=False)
        if response.status_code == 200:
            # 假设 `html_content` 是你获取的 HTML 页面内容
            html_content = response.text

            # 使用 BeautifulSoup 解析 HTML
            soup = BeautifulSoup(html_content, 'html.parser')

            # 获取所有下载链接
            download_links = soup.select('.variant')

            # 打印所有的链接
            for link in download_links:
                # print(link['href'])
                res_data.append(link['href'])
        else:
            res['msg'] = "请求失败"
    except Exception as e:
        res['msg'] = "请求失败"
    res['data'] = res_data
    return json.dumps(res)


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=6666, debug=True)
